{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "<table style=\"float:left; border:none\">\n",
    "   <tr style=\"border:none; background-color: #ffffff\">\n",
    "       <td style=\"border:none\">\n",
    "           <a href=\"http://bokeh.pydata.org/\">     \n",
    "           <img \n",
    "               src=\"assets/bokeh-transparent.png\" \n",
    "               style=\"width:50px\"\n",
    "           >\n",
    "           </a>    \n",
    "       </td>\n",
    "       <td style=\"border:none\">\n",
    "           <h1>Bokeh 教程</h1>\n",
    "       </td>\n",
    "   </tr>\n",
    "</table>\n",
    "\n",
    "<div style=\"float:right;\"><h2>08. 图画和网状图</h2></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"https://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"8af8b3e4-0710-4925-9013-0c8422920173\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(root) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
       "    root._bokeh_onload_callbacks = [];\n",
       "    root._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "  var JS_MIME_TYPE = 'application/javascript';\n",
       "  var HTML_MIME_TYPE = 'text/html';\n",
       "  var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n",
       "  var CLASS_NAME = 'output_bokeh rendered_html';\n",
       "\n",
       "  /**\n",
       "   * Render data to the DOM node\n",
       "   */\n",
       "  function render(props, node) {\n",
       "    var script = document.createElement(\"script\");\n",
       "    node.appendChild(script);\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when an output is cleared or removed\n",
       "   */\n",
       "  function handleClearOutput(event, handle) {\n",
       "    var cell = handle.cell;\n",
       "\n",
       "    var id = cell.output_area._bokeh_element_id;\n",
       "    var server_id = cell.output_area._bokeh_server_id;\n",
       "    // Clean up Bokeh references\n",
       "    if (id !== undefined) {\n",
       "      Bokeh.index[id].model.document.clear();\n",
       "      delete Bokeh.index[id];\n",
       "    }\n",
       "\n",
       "    if (server_id !== undefined) {\n",
       "      // Clean up Bokeh references\n",
       "      var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n",
       "      cell.notebook.kernel.execute(cmd, {\n",
       "        iopub: {\n",
       "          output: function(msg) {\n",
       "            var element_id = msg.content.text.trim();\n",
       "            Bokeh.index[element_id].model.document.clear();\n",
       "            delete Bokeh.index[element_id];\n",
       "          }\n",
       "        }\n",
       "      });\n",
       "      // Destroy server and session\n",
       "      var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n",
       "      cell.notebook.kernel.execute(cmd);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  /**\n",
       "   * Handle when a new output is added\n",
       "   */\n",
       "  function handleAddOutput(event, handle) {\n",
       "    var output_area = handle.output_area;\n",
       "    var output = handle.output;\n",
       "\n",
       "    // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n",
       "    if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n",
       "      return\n",
       "    }\n",
       "\n",
       "    var toinsert = output_area.element.find(`.${CLASS_NAME.split(' ')[0]}`);\n",
       "\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n",
       "      toinsert[0].firstChild.textContent = output.data[JS_MIME_TYPE];\n",
       "      // store reference to embed id on output_area\n",
       "      output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n",
       "    }\n",
       "    if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n",
       "      var bk_div = document.createElement(\"div\");\n",
       "      bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n",
       "      var script_attrs = bk_div.children[0].attributes;\n",
       "      for (var i = 0; i < script_attrs.length; i++) {\n",
       "        toinsert[0].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n",
       "      }\n",
       "      // store reference to server id on output_area\n",
       "      output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function register_renderer(events, OutputArea) {\n",
       "\n",
       "    function append_mime(data, metadata, element) {\n",
       "      // create a DOM node to render to\n",
       "      var toinsert = this.create_output_subarea(\n",
       "        metadata,\n",
       "        CLASS_NAME,\n",
       "        EXEC_MIME_TYPE\n",
       "      );\n",
       "      this.keyboard_manager.register_events(toinsert);\n",
       "      // Render to node\n",
       "      var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n",
       "      render(props, toinsert[0]);\n",
       "      element.append(toinsert);\n",
       "      return toinsert\n",
       "    }\n",
       "\n",
       "    /* Handle when an output is cleared or removed */\n",
       "    events.on('clear_output.CodeCell', handleClearOutput);\n",
       "    events.on('delete.Cell', handleClearOutput);\n",
       "\n",
       "    /* Handle when a new output is added */\n",
       "    events.on('output_added.OutputArea', handleAddOutput);\n",
       "\n",
       "    /**\n",
       "     * Register the mime type and append_mime function with output_area\n",
       "     */\n",
       "    OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n",
       "      /* Is output safe? */\n",
       "      safe: true,\n",
       "      /* Index of renderer in `output_area.display_order` */\n",
       "      index: 0\n",
       "    });\n",
       "  }\n",
       "\n",
       "  // register the mime type if in Jupyter Notebook environment and previously unregistered\n",
       "  if (root.Jupyter !== undefined) {\n",
       "    var events = require('base/js/events');\n",
       "    var OutputArea = require('notebook/js/outputarea').OutputArea;\n",
       "\n",
       "    if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n",
       "      register_renderer(events, OutputArea);\n",
       "    }\n",
       "  }\n",
       "\n",
       "  \n",
       "  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    root._bokeh_timeout = Date.now() + 5000;\n",
       "    root._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    var el = document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\");\n",
       "    if (el != null) {\n",
       "      el.textContent = \"BokehJS is loading...\";\n",
       "    }\n",
       "    if (root.Bokeh !== undefined) {\n",
       "      if (el != null) {\n",
       "        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n",
       "      }\n",
       "    } else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "\n",
       "  function run_callbacks() {\n",
       "    try {\n",
       "      root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
       "    }\n",
       "    finally {\n",
       "      delete root._bokeh_onload_callbacks\n",
       "    }\n",
       "    console.info(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(js_urls, callback) {\n",
       "    root._bokeh_onload_callbacks.push(callback);\n",
       "    if (root._bokeh_is_loading > 0) {\n",
       "      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    root._bokeh_is_loading = js_urls.length;\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var s = document.createElement('script');\n",
       "      s.src = url;\n",
       "      s.async = false;\n",
       "      s.onreadystatechange = s.onload = function() {\n",
       "        root._bokeh_is_loading--;\n",
       "        if (root._bokeh_is_loading === 0) {\n",
       "          console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
       "          run_callbacks()\n",
       "        }\n",
       "      };\n",
       "      s.onerror = function() {\n",
       "        console.warn(\"failed to load library \" + url);\n",
       "      };\n",
       "      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "    }\n",
       "  };var element = document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\");\n",
       "  if (element == null) {\n",
       "    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '8af8b3e4-0710-4925-9013-0c8422920173' but no matching script tag was found. \")\n",
       "    return false;\n",
       "  }\n",
       "\n",
       "  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "    },\n",
       "    function(Bokeh) {\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if ((root.Bokeh !== undefined) || (force === true)) {\n",
       "      for (var i = 0; i < inline_js.length; i++) {\n",
       "        inline_js[i].call(root, root.Bokeh);\n",
       "      }if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < root._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!root._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      root._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (root._bokeh_is_loading === 0) {\n",
       "    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(js_urls, function() {\n",
       "      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(window));"
      ],
      "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n  function now() {\n    return new Date();\n  }\n\n  var force = true;\n\n  if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n    root._bokeh_onload_callbacks = [];\n    root._bokeh_is_loading = undefined;\n  }\n\n  \n\n  \n  if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n    root._bokeh_timeout = Date.now() + 5000;\n    root._bokeh_failed_load = false;\n  }\n\n  var NB_LOAD_WARNING = {'data': {'text/html':\n     \"<div style='background-color: #fdd'>\\n\"+\n     \"<p>\\n\"+\n     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n     \"</p>\\n\"+\n     \"<ul>\\n\"+\n     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n     \"</ul>\\n\"+\n     \"<code>\\n\"+\n     \"from bokeh.resources import INLINE\\n\"+\n     \"output_notebook(resources=INLINE)\\n\"+\n     \"</code>\\n\"+\n     \"</div>\"}};\n\n  function display_loaded() {\n    var el = document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\");\n    if (el != null) {\n      el.textContent = \"BokehJS is loading...\";\n    }\n    if (root.Bokeh !== undefined) {\n      if (el != null) {\n        el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n      }\n    } else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(display_loaded, 100)\n    }\n  }\n\n\n  function run_callbacks() {\n    try {\n      root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n    }\n    finally {\n      delete root._bokeh_onload_callbacks\n    }\n    console.info(\"Bokeh: all callbacks have finished\");\n  }\n\n  function load_libs(js_urls, callback) {\n    root._bokeh_onload_callbacks.push(callback);\n    if (root._bokeh_is_loading > 0) {\n      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n      return null;\n    }\n    if (js_urls == null || js_urls.length === 0) {\n      run_callbacks();\n      return null;\n    }\n    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n    root._bokeh_is_loading = js_urls.length;\n    for (var i = 0; i < js_urls.length; i++) {\n      var url = js_urls[i];\n      var s = document.createElement('script');\n      s.src = url;\n      s.async = false;\n      s.onreadystatechange = s.onload = function() {\n        root._bokeh_is_loading--;\n        if (root._bokeh_is_loading === 0) {\n          console.log(\"Bokeh: all BokehJS libraries loaded\");\n          run_callbacks()\n        }\n      };\n      s.onerror = function() {\n        console.warn(\"failed to load library \" + url);\n      };\n      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n      document.getElementsByTagName(\"head\")[0].appendChild(s);\n    }\n  };var element = document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\");\n  if (element == null) {\n    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '8af8b3e4-0710-4925-9013-0c8422920173' but no matching script tag was found. \")\n    return false;\n  }\n\n  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-0.12.10.min.js\"];\n\n  var inline_js = [\n    function(Bokeh) {\n      Bokeh.set_log_level(\"info\");\n    },\n    \n    function(Bokeh) {\n      \n    },\n    function(Bokeh) {\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.10.min.css\");\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.10.min.css\");\n      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-0.12.10.min.css\");\n    }\n  ];\n\n  function run_inline_js() {\n    \n    if ((root.Bokeh !== undefined) || (force === true)) {\n      for (var i = 0; i < inline_js.length; i++) {\n        inline_js[i].call(root, root.Bokeh);\n      }if (force === true) {\n        display_loaded();\n      }} else if (Date.now() < root._bokeh_timeout) {\n      setTimeout(run_inline_js, 100);\n    } else if (!root._bokeh_failed_load) {\n      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n      root._bokeh_failed_load = true;\n    } else if (force !== true) {\n      var cell = $(document.getElementById(\"8af8b3e4-0710-4925-9013-0c8422920173\")).parents('.cell').data().cell;\n      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n    }\n\n  }\n\n  if (root._bokeh_is_loading === 0) {\n    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n    run_inline_js();\n  } else {\n    load_libs(js_urls, function() {\n      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n      run_inline_js();\n    });\n  }\n}(window));"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.io import show, output_notebook\n",
    "from bokeh.plotting import figure\n",
    "\n",
    "output_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "\n",
    "G = nx.desargues_graph()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div class=\"bk-root\">\n",
       "    <div class=\"bk-plotdiv\" id=\"9f2662ea-c8cd-4ac5-8447-059d48224852\"></div>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    var docs_json = {\"edaf0d84-c495-48e6-95f6-2883f382225e\":{\"roots\":{\"references\":[{\"attributes\":{\"callback\":null,\"end\":1.1,\"start\":-1.1},\"id\":\"5546e765-1d2a-46e5-8eda-7d6c2d7a0c27\",\"type\":\"Range1d\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\"},\"id\":\"763f4efb-da6f-4b75-9844-99d461af06e5\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"f97aabfe-e28f-44b7-bfb1-2d0d6628081b\",\"type\":\"NodesOnly\"},{\"attributes\":{\"callback\":null,\"end\":1.1,\"start\":-1.1},\"id\":\"a97b1664-6d6e-404f-a489-7486ce470746\",\"type\":\"Range1d\"},{\"attributes\":{\"renderers\":[{\"id\":\"36dc7010-3f55-49a6-ac69-8d4710174f45\",\"type\":\"GraphRenderer\"}],\"title\":{\"id\":\"3f3f09a8-6502-496c-a0e7-c0885f6669dc\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"763f4efb-da6f-4b75-9844-99d461af06e5\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"a97b1664-6d6e-404f-a489-7486ce470746\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"ebfebfc7-4b85-487c-9462-523bbc224586\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"5546e765-1d2a-46e5-8eda-7d6c2d7a0c27\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"01317fb0-8cc7-480c-b398-ad55163cd97d\",\"type\":\"LinearScale\"}},\"id\":\"a66ba7cb-aaf3-40f6-b5b8-5d0c5df947e8\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"ebfebfc7-4b85-487c-9462-523bbc224586\",\"type\":\"LinearScale\"},{\"attributes\":{\"edge_renderer\":{\"id\":\"328469b6-c77a-4565-b2e0-d344b6beeb78\",\"type\":\"GlyphRenderer\"},\"inspection_policy\":{\"id\":\"8c947c67-ee51-41a2-9d2f-ae87fbe4c5ff\",\"type\":\"NodesOnly\"},\"layout_provider\":{\"id\":\"54166028-a78d-403b-bff3-5cb0de3612d4\",\"type\":\"StaticLayoutProvider\"},\"node_renderer\":{\"id\":\"36946c50-9b41-4a15-a0b7-c8759f705030\",\"type\":\"GlyphRenderer\"},\"selection_policy\":{\"id\":\"f97aabfe-e28f-44b7-bfb1-2d0d6628081b\",\"type\":\"NodesOnly\"}},\"id\":\"36dc7010-3f55-49a6-ac69-8d4710174f45\",\"type\":\"GraphRenderer\"},{\"attributes\":{},\"id\":\"01317fb0-8cc7-480c-b398-ad55163cd97d\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"orange\"},\"size\":{\"units\":\"screen\",\"value\":20}},\"id\":\"eb0f1dc5-4b16-42ff-ac1f-52d2771e5c82\",\"type\":\"Circle\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"index\"],\"data\":{\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]}},\"id\":\"b96eea8f-6290-42d2-b7de-ab0a118c7e6e\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"b96eea8f-6290-42d2-b7de-ab0a118c7e6e\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"eb0f1dc5-4b16-42ff-ac1f-52d2771e5c82\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"3a9d0fc9-96ed-40d3-b499-4c08880194c2\",\"type\":\"CDSView\"}},\"id\":\"36946c50-9b41-4a15-a0b7-c8759f705030\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"b96eea8f-6290-42d2-b7de-ab0a118c7e6e\",\"type\":\"ColumnDataSource\"}},\"id\":\"3a9d0fc9-96ed-40d3-b499-4c08880194c2\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"05c09b39-69e0-4ea0-a0ca-1c4e55448a5c\",\"type\":\"MultiLine\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"start\",\"end\"],\"data\":{\"end\":[1,19,5,2,16,3,11,4,14,5,9,6,7,15,8,18,9,13,10,11,19,12,13,17,14,15,16,17,18,19],\"start\":[0,0,0,1,1,2,2,3,3,4,4,5,6,6,7,7,8,8,9,10,10,11,12,12,13,14,15,16,17,18]}},\"id\":\"6299d5ba-07d6-4034-b2be-8647552d77bd\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"6299d5ba-07d6-4034-b2be-8647552d77bd\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"05c09b39-69e0-4ea0-a0ca-1c4e55448a5c\",\"type\":\"MultiLine\"},\"hover_glyph\":null,\"muted_glyph\":null,\"view\":{\"id\":\"52138c31-df5f-442a-a1a6-31d042fecff0\",\"type\":\"CDSView\"}},\"id\":\"328469b6-c77a-4565-b2e0-d344b6beeb78\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"6299d5ba-07d6-4034-b2be-8647552d77bd\",\"type\":\"ColumnDataSource\"}},\"id\":\"52138c31-df5f-442a-a1a6-31d042fecff0\",\"type\":\"CDSView\"},{\"attributes\":{\"graph_layout\":{\"0\":[0.47989791510054974,1.517037371824551],\"1\":[0.01668802947442662,0.2923413191482944],\"10\":[-1.590339859236763,1.1117029396547569],\"11\":[-2.0,-0.03720961872990824],\"12\":[-1.3111403248481928,-1.0971158660794988],\"13\":[-0.4766109662398799,-1.5392224628798041],\"14\":[0.4190673395981764,-1.5166203949671873],\"15\":[1.597543589440645,-1.1061178800234452],\"16\":[0.8202600299198217,-0.7493851608531703],\"17\":[-0.07142824943755982,-0.8250796136254543],\"18\":[0.43842737674954824,0.4811955542963532],\"19\":[-0.41591746103781546,1.5412807650183977],\"2\":[-1.196050483999096,-0.04214230873815429],\"3\":[-0.456673144886659,-0.4405976010829892],\"4\":[0.041255163474644994,0.8375375637889719],\"5\":[1.301062338213754,1.090995813397066],\"6\":[1.9912496713435488,0.04907577907864058],\"7\":[1.2050423465585263,0.04743097352363837],\"8\":[0.021933800055423707,-0.33195838271947825],\"9\":[-0.8142671102430951,0.716851209968425]}},\"id\":\"54166028-a78d-403b-bff3-5cb0de3612d4\",\"type\":\"StaticLayoutProvider\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"3f3f09a8-6502-496c-a0e7-c0885f6669dc\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"8c947c67-ee51-41a2-9d2f-ae87fbe4c5ff\",\"type\":\"NodesOnly\"}],\"root_ids\":[\"a66ba7cb-aaf3-40f6-b5b8-5d0c5df947e8\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n",
       "    var render_items = [{\"docid\":\"edaf0d84-c495-48e6-95f6-2883f382225e\",\"elementid\":\"9f2662ea-c8cd-4ac5-8447-059d48224852\",\"modelid\":\"a66ba7cb-aaf3-40f6-b5b8-5d0c5df947e8\"}];\n",
       "\n",
       "    root.Bokeh.embed.embed_items(docs_json, render_items);\n",
       "  }\n",
       "\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        embed_document(root);\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "      attempts++;\n",
       "      if (attempts > 100) {\n",
       "        console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "a66ba7cb-aaf3-40f6-b5b8-5d0c5df947e8"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models.graphs import from_networkx\n",
    "from bokeh.models import Range1d, Plot\n",
    "\n",
    "# We could use figure here but don't want all the axes and titles\n",
    "plot = Plot(x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))\n",
    "\n",
    "# Create a Bokeh graph from the NetworkX input using nx.spring_layout\n",
    "graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))\n",
    "plot.renderers.append(graph)\n",
    "\n",
    "# Set some of the default node glyph (Circle) properties\n",
    "graph.node_renderer.glyph.update(size=20, fill_color=\"orange\")\n",
    "\n",
    "show(plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercise: try a different NetworkX layout, and set some `graph.edge_renderer` properties\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div class=\"bk-root\">\n",
       "    <div class=\"bk-plotdiv\" id=\"782035de-3e2e-45d0-a072-db705d0a0086\"></div>\n",
       "</div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "(function(root) {\n",
       "  function embed_document(root) {\n",
       "    var docs_json = {\"8cf88d7d-fe1f-4b42-8b7d-3e720e5ea782\":{\"roots\":{\"references\":[{\"attributes\":{\"line_color\":{\"value\":\"#abdda4\"},\"line_width\":{\"value\":4}},\"id\":\"f92dd43e-3349-41aa-81f1-71fb969d7e0c\",\"type\":\"MultiLine\"},{\"attributes\":{\"renderers\":[{\"id\":\"4cf9a9fc-df3d-42af-9154-29e09533dccd\",\"type\":\"GraphRenderer\"}],\"title\":{\"id\":\"18e6c10e-1768-46f7-b4ad-466bbbfac7db\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"ca16026d-3bc9-4287-8d5b-c02ee9307286\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"f20fe333-7c89-4f9d-8827-ebfa48d947a2\",\"type\":\"Range1d\"},\"x_scale\":{\"id\":\"6186c96e-0ed8-4890-8435-4b67108a56ec\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"9a035135-1521-4516-a1c8-520ff5f9b034\",\"type\":\"Range1d\"},\"y_scale\":{\"id\":\"4279b366-7d2a-4a3d-9140-a5197ad4744f\",\"type\":\"LinearScale\"}},\"id\":\"73012cce-96a3-4e36-b75a-99f8dc9d66d6\",\"type\":\"Plot\"},{\"attributes\":{\"line_alpha\":{\"value\":0.8},\"line_color\":{\"value\":\"#cccccc\"},\"line_width\":{\"value\":2}},\"id\":\"ca142754-e3ca-4244-a77e-e64cbfda29b1\",\"type\":\"MultiLine\"},{\"attributes\":{\"fill_color\":{\"value\":\"#abdda4\"},\"size\":{\"units\":\"screen\",\"value\":25}},\"id\":\"a92f15b1-fb03-4fe6-a8d3-8960d305a7e4\",\"type\":\"Circle\"},{\"attributes\":{\"callback\":null,\"end\":1.1,\"start\":-1.1},\"id\":\"9a035135-1521-4516-a1c8-520ff5f9b034\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"d506b42b-e196-483f-9a9e-aa8ba11d774f\",\"type\":\"NodesOnly\"},{\"attributes\":{\"callback\":null,\"end\":1.1,\"start\":-1.1},\"id\":\"f20fe333-7c89-4f9d-8827-ebfa48d947a2\",\"type\":\"Range1d\"},{\"attributes\":{},\"id\":\"78d8144f-44bc-4df7-a0cf-768f82ced939\",\"type\":\"NodesAndLinkedEdges\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"31e76336-ecb0-4ef5-8c17-cafcc794081a\",\"type\":\"HoverTool\"}]},\"id\":\"ca16026d-3bc9-4287-8d5b-c02ee9307286\",\"type\":\"Toolbar\"},{\"attributes\":{\"edge_renderer\":{\"id\":\"2d85af6e-51f2-4d87-8a36-84af969f13a8\",\"type\":\"GlyphRenderer\"},\"inspection_policy\":{\"id\":\"78d8144f-44bc-4df7-a0cf-768f82ced939\",\"type\":\"NodesAndLinkedEdges\"},\"layout_provider\":{\"id\":\"91e50a74-06b6-4114-ba7c-52ddcc40c62c\",\"type\":\"StaticLayoutProvider\"},\"node_renderer\":{\"id\":\"bc06e012-ff06-40e4-b083-54df98ad8733\",\"type\":\"GlyphRenderer\"},\"selection_policy\":{\"id\":\"d506b42b-e196-483f-9a9e-aa8ba11d774f\",\"type\":\"NodesOnly\"}},\"id\":\"4cf9a9fc-df3d-42af-9154-29e09533dccd\",\"type\":\"GraphRenderer\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"index\"],\"data\":{\"index\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33]}},\"id\":\"794bf347-41f8-4378-8834-f52bae6a3eed\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"794bf347-41f8-4378-8834-f52bae6a3eed\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"24217c18-a175-4d4c-970a-88d555080a27\",\"type\":\"Circle\"},\"hover_glyph\":{\"id\":\"a92f15b1-fb03-4fe6-a8d3-8960d305a7e4\",\"type\":\"Circle\"},\"muted_glyph\":null,\"view\":{\"id\":\"d4ba0f48-b3c8-4a66-8c7e-74b5a8c0212d\",\"type\":\"CDSView\"}},\"id\":\"bc06e012-ff06-40e4-b083-54df98ad8733\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"794bf347-41f8-4378-8834-f52bae6a3eed\",\"type\":\"ColumnDataSource\"}},\"id\":\"d4ba0f48-b3c8-4a66-8c7e-74b5a8c0212d\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"4279b366-7d2a-4a3d-9140-a5197ad4744f\",\"type\":\"LinearScale\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"start\",\"end\"],\"data\":{\"end\":[1,2,3,4,5,6,7,8,10,11,12,13,17,19,21,31,2,3,7,13,17,19,21,30,3,7,8,9,13,27,28,32,7,12,13,6,10,6,10,16,16,30,32,33,33,33,32,33,32,33,32,33,33,32,33,32,33,25,27,29,32,33,25,27,31,31,29,33,33,31,33,32,33,32,33,32,33,33],\"start\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,4,4,5,5,5,6,8,8,8,9,13,14,14,15,15,18,18,19,20,20,22,22,23,23,23,23,23,24,24,24,25,26,26,27,28,28,29,29,30,30,31,31,32]}},\"id\":\"68f79ad8-3a52-41bf-8f3f-c98905f88082\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"6186c96e-0ed8-4890-8435-4b67108a56ec\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"#2b83ba\"},\"size\":{\"units\":\"screen\",\"value\":25}},\"id\":\"24217c18-a175-4d4c-970a-88d555080a27\",\"type\":\"Circle\"},{\"attributes\":{\"data_source\":{\"id\":\"68f79ad8-3a52-41bf-8f3f-c98905f88082\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"ca142754-e3ca-4244-a77e-e64cbfda29b1\",\"type\":\"MultiLine\"},\"hover_glyph\":{\"id\":\"f92dd43e-3349-41aa-81f1-71fb969d7e0c\",\"type\":\"MultiLine\"},\"muted_glyph\":null,\"view\":{\"id\":\"5310819c-bb59-4a76-bea1-4169b88fa409\",\"type\":\"CDSView\"}},\"id\":\"2d85af6e-51f2-4d87-8a36-84af969f13a8\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"graph_layout\":{\"0\":[0.6795338983766972,-0.19653391961372985],\"1\":[0.3332887164296425,-0.30749741807507425],\"10\":[1.2648261924089035,-0.39306190859423307],\"11\":[1.356472044634541,0.11529194168184839],\"12\":[1.0136815154070482,0.23984945852281642],\"13\":[0.15040278263055482,-0.041038474843680506],\"14\":[-1.1842844777279835,-0.27362612717376705],\"15\":[-1.2549267616613438,-0.08423099996548793],\"16\":[2.0,-0.6543313697818679],\"17\":[0.5616649071348362,-0.7772160852851762],\"18\":[-0.9981037071638705,-0.3769475974078096],\"19\":[0.01356387097118641,-0.3685499144894538],\"2\":[-0.017553084912105877,0.07748242226723759],\"20\":[-1.253535462656989,0.11834970300136637],\"21\":[0.7542295324707391,-0.6380552205050871],\"22\":[-1.1137752607217914,0.5528349643806144],\"23\":[-0.8025950868628668,0.6432861442403631],\"24\":[-0.21019797329127937,1.082717913082763],\"25\":[-0.49779615860073395,1.0617527968260674],\"26\":[-1.452757023925228,0.3393008657256845],\"27\":[-0.41091128694481527,0.6454655887503428],\"28\":[-0.3735624263673341,0.37137689593022244],\"29\":[-1.1278258688963938,0.37600897779672515],\"3\":[0.5742061687539997,0.045699609204385636],\"30\":[-0.3545567048194319,-0.2612690603150088],\"31\":[-0.1556896512663086,0.46810122498611173],\"32\":[-0.7784187428439475,0.10740842067091488],\"33\":[-0.677389636312524,0.09261591129960249],\"4\":[1.1963971735317365,-0.67982768339013],\"5\":[1.5356229477548289,-0.3758364351643832],\"6\":[1.4723338613465065,-0.5976770998991285],\"7\":[0.4667836279686671,0.15535210637144262],\"8\":[-0.1723723878770027,-0.0984685196538004],\"9\":[-0.5367555369679359,-0.36872711058069274]}},\"id\":\"91e50a74-06b6-4114-ba7c-52ddcc40c62c\",\"type\":\"StaticLayoutProvider\"},{\"attributes\":{\"source\":{\"id\":\"68f79ad8-3a52-41bf-8f3f-c98905f88082\",\"type\":\"ColumnDataSource\"}},\"id\":\"5310819c-bb59-4a76-bea1-4169b88fa409\",\"type\":\"CDSView\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"18e6c10e-1768-46f7-b4ad-466bbbfac7db\",\"type\":\"Title\"},{\"attributes\":{\"callback\":null,\"tooltips\":null},\"id\":\"31e76336-ecb0-4ef5-8c17-cafcc794081a\",\"type\":\"HoverTool\"}],\"root_ids\":[\"73012cce-96a3-4e36-b75a-99f8dc9d66d6\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.10\"}};\n",
       "    var render_items = [{\"docid\":\"8cf88d7d-fe1f-4b42-8b7d-3e720e5ea782\",\"elementid\":\"782035de-3e2e-45d0-a072-db705d0a0086\",\"modelid\":\"73012cce-96a3-4e36-b75a-99f8dc9d66d6\"}];\n",
       "\n",
       "    root.Bokeh.embed.embed_items(docs_json, render_items);\n",
       "  }\n",
       "\n",
       "  if (root.Bokeh !== undefined) {\n",
       "    embed_document(root);\n",
       "  } else {\n",
       "    var attempts = 0;\n",
       "    var timer = setInterval(function(root) {\n",
       "      if (root.Bokeh !== undefined) {\n",
       "        embed_document(root);\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "      attempts++;\n",
       "      if (attempts > 100) {\n",
       "        console.log(\"Bokeh: ERROR: Unable to embed document because BokehJS library is missing\")\n",
       "        clearInterval(timer);\n",
       "      }\n",
       "    }, 10, root)\n",
       "  }\n",
       "})(window);"
      ],
      "application/vnd.bokehjs_exec.v0+json": ""
     },
     "metadata": {
      "application/vnd.bokehjs_exec.v0+json": {
       "id": "73012cce-96a3-4e36-b75a-99f8dc9d66d6"
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from bokeh.models.graphs import NodesAndLinkedEdges\n",
    "from bokeh.models import Circle, HoverTool, MultiLine\n",
    "\n",
    "G = nx.karate_club_graph()\n",
    "\n",
    "# We could use figure here but don't want all the axes and titles\n",
    "plot = Plot(x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))\n",
    "\n",
    "# Create a Bokeh graph from the NetworkX input using nx.spring_layout\n",
    "graph = from_networkx(G, nx.spring_layout, scale=2, center=(0,0))\n",
    "plot.renderers.append(graph)\n",
    "\n",
    "# Blue circles for nodes, and light grey lines for edges\n",
    "graph.node_renderer.glyph = Circle(size=25, fill_color='#2b83ba')\n",
    "graph.edge_renderer.glyph = MultiLine(line_color=\"#cccccc\", line_alpha=0.8, line_width=2)\n",
    "\n",
    "# green hover for both nodes and edges\n",
    "graph.node_renderer.hover_glyph = Circle(size=25, fill_color='#abdda4')\n",
    "graph.edge_renderer.hover_glyph = MultiLine(line_color='#abdda4', line_width=4)\n",
    "\n",
    "# When we hover over nodes, highlight adjecent edges too\n",
    "graph.inspection_policy = NodesAndLinkedEdges()\n",
    "\n",
    "plot.add_tools(HoverTool(tooltips=None))\n",
    "\n",
    "show(plot)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Exercise: try a different inspection (or selection) policy\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
